package net.oschina.victor.liu.lemon.common.shiro;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import net.oschina.victor.liu.lemon.entity.User;

/**
 * 此类用于保存用户时，将界面接收到的用户密码进行加密存储到数据库当中
 * 
 * @author Victor
 *
 */
public class PasswordHelper {
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private String algorithmName = "md5";
	private final int hashIterations = 2;

	public void encryptPassword(User user) {
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		String newPassword = new SimpleHash(algorithmName, user.getPassword(),
				ByteSource.Util.bytes(user.getCredentialsSalt()), hashIterations).toHex();
		user.setPassword(newPassword);
		System.out.println("数据库中的getCredentialsSalt" + user.getCredentialsSalt());
		System.out.println("数据库中的Password" + user.getPassword());
		System.out.println("数据库中的Salt" + user.getSalt());

	}
}